package com.gxp.web.controller;

import com.gxp.common.annotation.Audit;
import com.gxp.common.core.controller.BaseController;
import com.gxp.common.core.domain.AjaxResult;
import com.gxp.common.core.page.TableDataInfo;
import com.gxp.common.enums.BusinessAction;
import com.gxp.common.enums.BusinessTitle;
import com.gxp.common.utils.DateUtils;
import com.gxp.common.utils.DictUtils;
import com.gxp.system.domain.ZkInstrument;
import com.gxp.system.service.IZkInstrumentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 仪器Controller
 * 
 * @author zh
 * @date 2024-03-08
 */
@Controller
@RequestMapping("/system/instrument")
public class ZkInstrumentController extends BaseController
{
    private String prefix = "system/instrument";

    @Autowired
    private IZkInstrumentService zkInstrumentService;

    @RequiresPermissions("system:instrument:view")
    @GetMapping()
    public String instrument()
    {
        return prefix + "/instrument";
    }

    /**
     * 查询仪器列表
     */
    @RequiresPermissions("system:instrument:view")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ZkInstrument zkInstrument)
    {
        startPage();
        List<ZkInstrument> list = zkInstrumentService.selectZkInstrumentList(zkInstrument);
        return getDataTable(list);
    }

    /**
     * 新增仪器
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存仪器
     */
    @RequiresPermissions("system:instrument:add")
    @Audit(businessTitle = BusinessTitle.INSTRUMENT, businessAction = BusinessAction.CREATED, description = "新增仪器")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZkInstrument zkInstrument)
    {
        zkInstrument.setCreateBy(getLoginName());
        int row = zkInstrumentService.insertZkInstrument(zkInstrument);
        return toAjax(row, zkInstrument.getInstId());
    }

    /**
     * 修改仪器
     */
    @RequiresPermissions("system:instrument:edit")
    @GetMapping("/edit/{instId}")
    public String edit(@PathVariable("instId") Long instId, ModelMap mmap)
    {
        ZkInstrument zkInstrument = zkInstrumentService.selectZkInstrumentByInstId(instId);
        mmap.put("zkInstrument", zkInstrument);
        return prefix + "/edit";
    }

    /**
     * 修改保存仪器
     */
    @RequiresPermissions("system:instrument:edit")
    @Audit(businessTitle = BusinessTitle.INSTRUMENT, businessAction = BusinessAction.UPDATED, description = "仪器修改")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZkInstrument zkInstrument)
    {
        zkInstrument.setUpdateBy(getLoginName());
        return toAjax(zkInstrumentService.updateZkInstrument(zkInstrument), zkInstrument.getInstId());
    }

    /**
     * 删除仪器
     */
    @RequiresPermissions("system:instrument:remove")
    @Audit(businessTitle = BusinessTitle.INSTRUMENT, businessAction = BusinessAction.DELETED, description = "仪器删除")
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(zkInstrumentService.deleteZkInstrumentByInstIds(ids));
    }

    /**
     * 仪器状态修改
     */
    @Audit(businessTitle = BusinessTitle.INSTRUMENT, businessAction = BusinessAction.UPDATED, description = "仪器状态改变")
    @RequiresPermissions("system:instrument:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(ZkInstrument instrument)
    {
        return toAjax(zkInstrumentService.changeStatus(instrument), instrument.getInstId());
    }

    /**
     * 仪器重名校验
     */
    @PostMapping("/checkInstNameUnique")
    @ResponseBody
    public boolean checkInstNameUnique(ZkInstrument instrument)
    {
        return zkInstrumentService.checkInstNameUnique(instrument);
    }

    /**
     * hostId重复检验
     */
    @PostMapping("/checkHostIdUnique")
    @ResponseBody
    public boolean checkHostIdUnique(ZkInstrument instrument)
    {
        return zkInstrumentService.checkHostIdUnique(instrument);
    }

    /**
     * 实验模板列表
     */
    @GetMapping("/templates/{instId}")
    public String templates(@PathVariable("instId") Long instId, ModelMap mmap){
        // 获取仪器状态
        ZkInstrument zkInstrument = zkInstrumentService.selectZkInstrumentByInstId(instId);
        mmap.addAttribute("instrument", zkInstrument);
        return prefix + "/template/template";
    }


    @RequiresPermissions("system:instrument:approve")
    @GetMapping("/approve")
    public String approve(ZkInstrument instrument, ModelMap mmap){
        ZkInstrument zkInstrument = zkInstrumentService.selectZkInstrumentByInstId(instrument.getInstId());
        mmap.addAttribute("zkInstrument", zkInstrument);
        return prefix + "/approve";
    }

    @Audit(businessTitle = BusinessTitle.INSTRUMENT, businessAction = BusinessAction.APPROVED, description = "仪器审核")
    @RequiresPermissions("system:instrument:approve")
    @PostMapping("/approve")
    @ResponseBody
    public AjaxResult approve(ZkInstrument instrument){
        instrument.setApproveBy(getLoginName());
        instrument.setApproveTime(DateUtils.getNowDate());
        String approveStatus = DictUtils.getDictLabel("sys_appr_status",instrument.getStatus().toString());
        return toAjax(zkInstrumentService.approve(instrument), approveStatus, instrument.getInstId());
    }
}
